package controller;

import aop.RequiredPermission;
import com.github.pagehelper.PageInfo;
import entity.LayRequest;
import entity.MyResult;
import entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import service.RoleService;

import java.util.List;

@Controller
@RequestMapping("/role")//设定该类中所有请求的统一访问前缀
public class RoleController {
    //注入service层对象；
    @Autowired
    private RoleService roleService;

    @RequestMapping("/add")
    public String insert(Role role){
        System.out.println("进入控制层方法");
        boolean flag = roleService.insert(role);
        return "redirect:list";
    }
    @ResponseBody
    @RequestMapping("/add2")
    public MyResult add2(Role role){
        System.out.println("进入控制层方法");
        boolean flag = roleService.insert(role);
        return new MyResult<>(0);
    }
    @RequiredPermission("role:list")
    @RequestMapping("/list")
    public String list(ModelMap modelMap){
        //查询所有角色
        List<Role> roles=roleService.selectAll();
        //将角色集合传递到前台页面
        modelMap.addAttribute("roles",roles);
        return "list.jsp";
    }
    @RequestMapping("/toadd")
    public String toadd(){
        return "add.jsp";
    }
    @RequestMapping("/delete")
    public String delete(Integer id){
        boolean flag=roleService.delete(id);
        return "redirect:list";
    }


    @RequestMapping("/toupdate")
    public String toupdate(Integer id,ModelMap modelMap){
        Role role=roleService.selectById(id);
        modelMap.addAttribute("role",role);
        return "update.jsp";
    }
    @RequiredPermission("role:update")
    @RequestMapping("/update")
    public String update(Role role){
        int i=roleService.update(role);
        return "redirect:list";
    }
    @ResponseBody
    @RequestMapping("/update2")
    public MyResult update2(Role role){
        int flag=roleService.update(role);
        return new MyResult(0);
    }
    @ResponseBody
    @GetMapping("/layList")
    public MyResult<List<Role>> layList(LayRequest layRequest){
        PageInfo<Role> rolePageInfo=roleService.selectByPage(layRequest);
        return new MyResult<>(0,rolePageInfo.getTotal(),rolePageInfo.getList());
    }
    @ResponseBody
    @PostMapping("/batchDelete")
    public MyResult batchDelete(Integer [] ids){
        roleService.batchDelete(ids);
        return new MyResult(0);
    }
}
